package com.longxi.tianjintianbao.ui.adapter.multitype.patrol;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.model.patrol.PatrolContent;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class PatrolContentViewBinder2 extends ItemViewBinder<PatrolContent, PatrolContentViewBinder2.ViewHolder> {

    OnSelectPatrolContentListener listener;
    Activity mContext;

    public PatrolContentViewBinder2(Activity activity, OnSelectPatrolContentListener listener) {
        this.mContext = activity;
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_patrol_content, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull PatrolContent patrolContent) {
        holder.mTvItem.setText("巡检项目:" + patrolContent.getItem());
        holder.mTvItemcontent.setText("项目内容:" + patrolContent.getItemcontent());
        holder.mLlBtn.setOnClickListener(v -> listener.onSuccess(holder.getAdapterPosition()));
        if(!TextUtils.isEmpty(patrolContent.getExplain())){
            holder.mTvExplain.setVisibility(View.VISIBLE);
            holder.mTvExplain.setText("巡检情况:" + patrolContent.getState()+ "\n巡检情况:" + "已完成" + "\n完成说明:" + patrolContent.getExplain());
        }else{
            holder.mTvExplain.setVisibility(View.GONE);
        }

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.head_icon)
                .error(R.mipmap.head_icon);

        if(!"".equals(patrolContent.getImage1())){
            holder.mIvImage1.setVisibility(View.VISIBLE);
            Glide.with(holder.mIvImage1.getContext())
                    .asBitmap()
                    .apply(options)
                    .load(patrolContent.getImage1())
                    .into(holder.mIvImage1);
            holder.selectList.add(new LocalMedia(patrolContent.getImage1(), 0, 0, ""));
            holder.mIvImage1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PictureSelector.create(mContext).themeStyle(R.style.picture_default_style).openExternalPreview(0, holder.selectList);
                }
            });
        }else{
            holder.mIvImage1.setVisibility(View.GONE);
        }
        if(!"".equals(patrolContent.getImage2())){
            holder.mIvImage2.setVisibility(View.VISIBLE);
            Glide.with(holder.mIvImage2.getContext())
                    .asBitmap()
                    .apply(options)
                    .load(patrolContent.getImage2())
                    .into(holder.mIvImage2);
            holder.selectList.add(new LocalMedia(patrolContent.getImage2(), 0, 0, ""));
            holder.mIvImage2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PictureSelector.create(mContext).themeStyle(R.style.picture_default_style).openExternalPreview(1, holder.selectList);
                }
            });
        }else{
            holder.mIvImage2.setVisibility(View.GONE);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mLlBtn;
        TextView mTvItem;
        TextView mTvItemcontent;
        ImageView mIvImage1;
        ImageView mIvImage2;
        TextView mTvExplain;
        List<LocalMedia> selectList;
        ViewHolder(View itemView) {
            super(itemView);
            mLlBtn = itemView.findViewById(R.id.llBtn);
            mTvItem = itemView.findViewById(R.id.tvItem);
            mTvItemcontent = itemView.findViewById(R.id.tvItemcontent);
            mIvImage1 = itemView.findViewById(R.id.ivImage1);
            mIvImage2 = itemView.findViewById(R.id.ivImage2);
            mTvExplain = itemView.findViewById(R.id.tvExplain);
            selectList = new ArrayList<>();
        }
    }

    public interface OnSelectPatrolContentListener {
        void onSuccess(int position);
    }

}
